package com.chushouya.product.service.admin.impl;

import com.chushouya.common.support.Contexts;
import com.general.framework.core.enums.StatusEnum;
import com.general.framework.core.lang.Beans;
import com.general.framework.core.lang.Check;
import com.general.framework.core.lang.Dates;
import com.general.framework.data.redis.RedisCache;
import com.chushouya.product.dao.entity.ProductSeriesEntity;
import com.chushouya.product.dao.repository.ProductSeriesRepository;
import com.chushouya.product.dto.admin.series.SeriesDetailDTO;
import com.chushouya.product.dto.admin.series.SeriesListDTO;
import com.chushouya.product.dto.admin.series.SeriesQuery;
import com.chushouya.product.service.admin.ProductSeriesService;
import com.chushouya.product.service.admin.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
@Slf4j
public class ProductSeriesServiceImpl implements ProductSeriesService {

    @Resource
    private ProductSeriesRepository productSeriesRepository;

    @Resource
    private ProductService productService;

    @Resource
    private RedisCache redisCache;

    @Override
    public List<SeriesListDTO> listProductSeries(SeriesQuery query) {
        return Beans.copyList(productSeriesRepository.selectList(query), SeriesListDTO.class);
    }

    @Override
    public SeriesDetailDTO getSeriesDetail(Long seriesId) {
        Check.notNull(seriesId, "系列Id不能为空");
        return Beans.copy(productSeriesRepository.selectByPrimaryKey(seriesId), SeriesDetailDTO.class);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long addSeries(SeriesDetailDTO series) {
        ProductSeriesEntity productSeriesEntity = Beans.copy(series, ProductSeriesEntity.class);
        if (productSeriesEntity.getStatus() == null) {
            productSeriesEntity.setStatus(StatusEnum.normal.value());
        }
        productSeriesEntity.setCreateTime(Dates.getTimeNow());
        productSeriesEntity.setUpdateTime(Dates.getTimeNow());
        productSeriesEntity.setHandleUser(Contexts.getUserContext().getUserName());
        productSeriesRepository.insertSelective(productSeriesEntity);
        this.clearCache();
        return productSeriesEntity.getSeriesId();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long editSeries(SeriesDetailDTO series) {
        Check.notNull(series.getSeriesId(), "系列Id不能为空");
        Check.notEmpty(series.getSeriesName(), "系列名称不能为空");
        final ProductSeriesEntity ProductSeriesEntity = productSeriesRepository.selectByPrimaryKey(series.getSeriesId());
        Check.notNull(ProductSeriesEntity, "分类不存在");
        ProductSeriesEntity updateEntity = Beans.copy(series, ProductSeriesEntity.class);
        updateEntity.setUpdateTime(Dates.getTimeNow());
        updateEntity.setHandleUser(Contexts.getUserContext().getUserName());
        productSeriesRepository.updateByPrimaryKeySelective(updateEntity);
        this.clearCache();
        return updateEntity.getSeriesId();
    }

    @Override
    public void removeSeries(Long seriesId) {
        Check.notNull(seriesId, "系列Id不能为空");
        Check.notTrue(productService.hasProductBindSeries(seriesId), "有商品关联当前系列，不能删除");
        productSeriesRepository.deleteByPrimaryKey(seriesId);
        this.clearCache();
    }

    @Override
    public Long editSeriesStatus(Long seriesId, Integer status) {
        Check.notNull(seriesId, "categoryId can not be null");
        Check.notNull(status, "status can not be null");
        ProductSeriesEntity updateEntity = new ProductSeriesEntity();
        updateEntity.setSeriesId(seriesId);
        updateEntity.setStatus(status);
        updateEntity.setUpdateTime(Dates.getTimeNow());
        updateEntity.setHandleUser(Contexts.getUserContext().getUserName());
        productSeriesRepository.updateByPrimaryKeySelective(updateEntity);
        this.clearCache();
        return updateEntity.getSeriesId();
    }

    @Override
    public String getSeriesName(Long seriesId) {
        return productSeriesRepository.selectByPrimaryKey(seriesId).getSeriesName();
    }

    private void clearCache() {
//        redisCache.keys( PRODUCT_SERIES.concat("*")).forEach(key -> redisCache.deleteObject(key));
    }

}
